package controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UserController extends Controller {

	public void index(){
		System.out.println("进来？");
		String name = getPara("name");
		String sex = getPara("sex");
		String address = getPara("address");
		String phone = getPara("phone");
		String email = getPara("email");
		String error = "";
		String temp = "";
		if(sex.equals("男") || sex.equals("女")){

		}else{
			error += "性别请输入男，女。";
		}
		if(phone.length() == 11 && phoneNoVerify(phone)){

		}else{
			error += "请输入13，15，18开头的十一位手机号码。";
		}
		if(emailVerify(email)){

		}else{
			error += "请输入正确的邮箱地址。";
		}
		System.out.println("属性："+name+sex+address+phone+email);
		System.out.println(error);
		if(error.equals("")){
			System.out.println("存入数据库。");
			Record record = new Record().set("name",name).set("sex",sex).set("address",address).set("phone",phone).set("email",email);
			Db.save("user",record);
		}else{
			temp = JsonKit.toJson(error);
		}
		renderJson(temp);
		
	}

	public void listUser(){
		String name = getPara("name");
		List<Record> user = null;
		if(name.equals("")){
			//显示全部用户
			System.out.println("全部用户");
			user = Db.find("select * from user");
		}else{
			//处理正常用户
			user = Db.find("select * from user where name=?",name);
		}
		setAttr("user",user);
		render("/WEB-INF/select.html");
	}

	
	//修改用户个人信息
	public void updateUser(){
		String id = getPara("id");
		System.out.println("id:"+id);
		Record user = Db.findFirst("select * from user where id=?",id);
		setAttr("user",user);
		render("/WEB-INF/update.html");
	}
	
	//修改
	public void update(){
		String id = (getPara("id"));
		String name = getPara("name");
		String sex = getPara("sex");
		String address = getPara("address");
		String phone = getPara("phone");
		String email = getPara("email");
		
		String error = "";
		String temp = "";
		if(sex.equals("男") || sex.equals("女")){

		}else{
			error += "性别请输入男，女。";
		}
		if(phone.length() == 11 && phoneNoVerify(phone)){

		}else{
			error += "请输入13，15，18开头的十一位手机号码。";
		}
		if(emailVerify(email)){

		}else{
			error += "请输入正确的邮箱地址。";
		}
		
		if(error.equals("")){
			System.out.println("更新？？？？");
			Db.update("update user set name=?,sex=?,address=?,phone=?,email=? where id=?",name,sex,address,phone,email,id);
		}else{
			temp = JsonKit.toJson(error);
		}
		renderJson(temp);
	}
	
	//删除用户
	public void deleteUser(){
		String id = getPara("id");
		System.out.println("删除用户："+ id);
		Db.update("delete from user where id=?",id);
	}
	
	//导出excel
	public void export() throws FileNotFoundException{
		System.out.println("导出excel。");
		
		OutputStream out = new FileOutputStream("F://通讯录名单.xls");
		
		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet("学生名单");
		HSSFCellStyle cellStyle = book.createCellStyle();	
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
		cellStyle.setWrapText(true);
		
		HSSFFont font = book.createFont();
		cellStyle.setFont(font);
		HSSFRow row0 = sheet.createRow(0);
		row0.setHeight((short) (3*256));
		HSSFCell cell01 = row0.createCell(0);
		HSSFCell cell02 = row0.createCell(1);
		HSSFCell cell03 = row0.createCell(2);
		HSSFCell cell04 = row0.createCell(3);
		HSSFCell cell05 = row0.createCell(4);
		HSSFCell cell06 = row0.createCell(5);
		cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell02.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell03.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell04.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell05.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell06.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell01.setCellStyle(cellStyle);
		cell02.setCellStyle(cellStyle);
		cell03.setCellStyle(cellStyle);
		cell04.setCellStyle(cellStyle);
		cell05.setCellStyle(cellStyle);
		cell06.setCellStyle(cellStyle);
		cell01.setCellValue("序号");
		cell02.setCellValue("姓名"); 
		cell03.setCellValue("性别");
		cell04.setCellValue("地址");
		cell05.setCellValue("电话");
		cell06.setCellValue("邮箱");
		
		List<Record> user = Db.find("select * from user");
		
		for(int i = 0;i < user.size();i++){
			HSSFRow row = sheet.createRow(i+1);
			HSSFCell cell0 = row.createCell(0);
			HSSFCell cell1 = row.createCell(1);
			HSSFCell cell2 = row.createCell(2);
			HSSFCell cell3 = row.createCell(3);
			HSSFCell cell4 = row.createCell(4);
			HSSFCell cell5 = row.createCell(5);
			
			cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
			
			cell0.setCellStyle(cellStyle);
			cell1.setCellStyle(cellStyle);
			cell2.setCellStyle(cellStyle);
			cell3.setCellStyle(cellStyle);
			cell4.setCellStyle(cellStyle);
			cell5.setCellStyle(cellStyle);
			
			cell0.setCellValue(i+1);
			cell1.setCellValue(user.get(i).getStr("name"));
			cell2.setCellValue(user.get(i).getStr("sex"));
			cell3.setCellValue(user.get(i).getStr("address"));
			cell4.setCellValue(user.get(i).getStr("phone"));
			cell5.setCellValue(user.get(i).getStr("email"));
		}
			
		try {
			book.write(out);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
				render("/WEB-INF/index.html");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
 }
		
	
	//手机号码验证
	public boolean phoneNoVerify(String str){

		Pattern check = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");  
		Matcher matcher = check.matcher(str);  
		System.out.println(matcher.matches()+"---"); 
		return matcher.matches();
	}

	//验证邮箱
	public boolean emailVerify(String email){
		boolean flag = false;
		try{
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		}catch(Exception e){
			flag = false;
		}
		return flag;
	}
}
